package com.cindata

import com.cindata.dictionary.*
import com.cindata.dimension.RiskDimension
import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class WorkflowInstanceConditionController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond WorkflowInstanceCondition.list(params), model:[workflowInstanceConditionCount: WorkflowInstanceCondition.count()]
    }

    def show(WorkflowInstanceCondition workflowInstanceCondition) {
        respond workflowInstanceCondition
    }

    def create() {
        params['targetUri'] = request.getHeader("referer")
        def workflowInstanceStage = WorkflowInstanceStage.findById(params['workflowInstanceStage'])
        def workflowInstanceStages = WorkflowInstanceStage.findAllByInstance(workflowInstanceStage?.instance)
        respond new WorkflowInstanceCondition(params), model: [workflowInstanceStages: workflowInstanceStages]
    }

    @Transactional
    def save(WorkflowInstanceCondition workflowInstanceCondition) {
        if (workflowInstanceCondition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (workflowInstanceCondition.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond workflowInstanceCondition.errors, view:'create'
            return
        }

        workflowInstanceCondition.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'workflowInstanceCondition.label', default: 'WorkflowInstanceCondition'), workflowInstanceCondition.id])
                redirect url: params['targetUri']
            }
            '*' { respond workflowInstanceCondition, [status: CREATED] }
        }
    }

    def edit(WorkflowInstanceCondition workflowInstanceCondition) {
        params['targetUri'] = request.getHeader("referer")
        def workflowInstanceStages = WorkflowInstanceStage.findAllByInstance(workflowInstanceCondition?.stage?.instance)
        respond workflowInstanceCondition, model: [workflowInstanceStages: workflowInstanceStages]
    }

    @Transactional
    def update(WorkflowInstanceCondition workflowInstanceCondition) {
        if (workflowInstanceCondition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (workflowInstanceCondition.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond workflowInstanceCondition.errors, view:'edit'
            return
        }

        workflowInstanceCondition.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'workflowInstanceCondition.label', default: 'WorkflowInstanceCondition'), workflowInstanceCondition.id])
                redirect url: params['targetUri']
            }
            '*'{ respond workflowInstanceCondition, [status: OK] }
        }
    }

    @Transactional
    def delete(WorkflowInstanceCondition workflowInstanceCondition) {
        def targetUri = request.getHeader("referer")
        if (workflowInstanceCondition == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        workflowInstanceCondition.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'workflowInstanceCondition.label', default: 'WorkflowInstanceCondition'), workflowInstanceCondition.id])
                redirect url: targetUri
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'workflowInstanceCondition.label', default: 'WorkflowInstanceCondition'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
